home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.5 Applications 2001 May / SGI IRIX 6.5 Applications 2001 May.iso / dev / insight_dev.idb / usr / lib / Insight / dweb / dtl2html / splithtml.z / splithtml
Encoding:
Text File  |  2001-04-05  |  15.0 KB  |  611 lines

  1. #!/usr/bin/perl
  2. #
  3. #  splithtml {-c cfg_file} {-l lvl_id} {-f fname_spec} {-v} {-noclean} input_f
  4. #
  5. #  Based on a given level identifier (1,2,3...), matching what is 
  6. #  found in the TOC data file (default.dat), this sub-program splits
  7. #  up the full HTML file into sep. HTML files. It also handles the
  8. #  updating of any and all references (including those in the TOC 
  9. #  data files).
  10. #
  11. #  requires perl5
  12.  
  13. # pull in the default configuration file; all local referenced
  14. # config files override whatever is in here
  15. #
  16.  
  17. $| = 1;
  18.  
  19. my($prog_dir) = $0;
  20. if( $prog_dir =~ /\// ) {
  21.     $prog_dir =~ s/splithtml$//; 
  22.     $prog_dir .= 'splithtml.cfg';
  23.     require "$prog_dir";
  24. } else {
  25.     require 'splithtml.cfg';
  26. }
  27.  
  28. $_fname_spec = '';
  29. $_lvl_id     = -1; 
  30. $_bVerbose   =  0;
  31. $_bClean     =  1;
  32. $_bHelpExist =  0;
  33. $_indxExist  = '';
  34. $_output_dir = '';
  35. $_input_file = '';
  36.  
  37. $_input_dir  = '';
  38. $_ttl        = '';
  39. $_curr_title = '';
  40.  
  41. %_element_map    = ();
  42.  
  43. @_break_elements = ();
  44. @_break_titles   = ();
  45.  
  46.  
  47. # read in cmd-line arguments
  48. #
  49. while(1) {
  50.  
  51.      if ($ARGV[0] eq "-c") {
  52.            shift(@ARGV);
  53.            require "$ARGV[0]";
  54.            shift(@ARGV);
  55.      } elsif ($ARGV[0] eq "-l") {
  56.            shift(@ARGV);
  57.            $_lvl_id= ($ARGV[0] + 0);
  58.            shift(@ARGV);
  59.      } elsif ($ARGV[0] eq "-f") {
  60.            shift(@ARGV);
  61.            $_fname_spec = $ARGV[0];
  62.            shift(@ARGV);
  63.      } elsif ($ARGV[0] eq "-o") {
  64.            shift(@ARGV);
  65.            $_output_dir = $ARGV[0];
  66.            shift(@ARGV);
  67.      } elsif ($ARGV[0] eq "-v") {
  68.            $_bVerbose = 1;
  69.            shift(@ARGV);
  70.      } elsif ($ARGV[0] eq "-noclean") {
  71.            $_bClean = 0;
  72.            shift(@ARGV);
  73.      } else {
  74.            last;
  75.      }
  76. }
  77. $_input_file = $ARGV[(@ARGV + 0) - 1];
  78.  
  79.  
  80. # see if any problems with arguments, as specified; also set up defaults
  81. #
  82. if( $_input_file eq '' ) {
  83.       &usage();
  84.       exit(0);
  85. }
  86.  
  87. my($i) = rindex($_input_file, "/");
  88. if($i == -1) {
  89.      $_input_dir = ".";
  90. } else {
  91.      $_input_dir = substr($_input_file, 0, $i);
  92. }
  93.  
  94. if( $_output_dir eq '' ) {
  95.     $_output_dir = $_input_dir;
  96. }
  97.  
  98. if( !(-w $_output_dir) ) {
  99.        &usage();
  100.        print "ERROR: Cannot write to $_output_dir\n";
  101.        exit(-1);
  102. }
  103.  
  104. if( $_fname_spec eq '' ) {
  105.       $_fname_spec = $DEF_FNAME_SPEC;
  106. }
  107. $_fname_spec =~ s/^\///;
  108.  
  109. if( $_lvl_id < 0 ) {
  110.       $_lvl_id = $DEF_CHUNK_ID;
  111. }
  112. if( $_lvl_id <= 0 || $_lvl_id > 5 ) {
  113.       $_lvl_id = 1;
  114. }
  115.  
  116. if( $_bVerbose == 1 ) {
  117.       print "\n\nsplithtml: will read from $_input_dir\n\n";
  118.       print "splithtml: processing $_input_dir/default.dat\n\n";
  119. }
  120.  
  121.  
  122. # read in table of contents data file, configure what the filename(s) will be
  123. #
  124. if( &readToc("$_input_dir/default.dat") == 0 ) {
  125.       &usage();
  126.       print "ERROR: Cannot read from $_input_dir/default.dat\n";
  127.       exit(-1);
  128. }
  129. if( $_bVerbose == 1 ) {
  130.      print "splithtml: ", (@_break_elements+0), " breaking elements found\n\n";
  131.      print "splithtml: processing $_input_file\n\n";
  132. }
  133.  
  134.  
  135. # read the master html file and break it up, fixing href's along the way
  136. #
  137. if( &createContentFiles($_input_file) == 0 ) {
  138.       &usage();
  139.       print "ERROR: Cannot read from $_input_file\n";
  140.       exit(-1);
  141. }
  142.  
  143.  
  144. # build the toc structures/files
  145. #
  146. if( $_bVerbose == 1 ) {
  147.      print "splithtml: processing TOC file(s)\n\n";
  148. }
  149.  
  150. &createHelpTopics();
  151.  
  152. if( &createTocFiles() == 0 ) {
  153.       &usage();
  154.       print "ERROR: Cannot create TOC file(s)\n";
  155.       exit(-1);
  156. }
  157.  
  158.  
  159. # clean up
  160. #
  161. if( $_bClean == 1 ) {
  162.      if( $_bVerbose == 1 ) {
  163.           print "splithtml: cleaning up...\n\n";
  164.      }
  165.      
  166.      my($cmd) = "/bin/rm -f $_input_dir/\*.dat $_input_file";
  167.      system($cmd);
  168.      print "ERROR: ($!) executing '$cmd'\n" if ($?);
  169. }
  170.  
  171.  
  172. exit(0);
  173.  
  174.  
  175.  
  176. #
  177. # void usage()
  178. #
  179. sub usage {
  180.  
  181.     print "\n\n",
  182.  
  183.  "usage: splithtml {-c cfg_file} {-l lvl_id} {-f fname_spec}\n",
  184.  "                           {-o output_dir} {-v} {-noclean} in_html_file\n\n",
  185.  "-c cfg_file    : is the configuration file to use. See the default file:\n",
  186.  "                 /usr/lib/Insight/dweb/dtl2html/splithtml.cfg for info.\n\n",
  187.  "-l lvl_id      : is the level to break/chunk on (based on TOC)\n",
  188.  "                 default used is \"1\"\n\n",
  189.  "-f fname_spec  : is the filename convention to use, must have a\n",
  190.  "                 \"%d\" within the string, as in \"sgi%05d.html\"\n",
  191.  "                 which serves as the default is not provided.\n\n",
  192.  "-o output_dir  : location to write the output files to, default is the\n",
  193.  "                 same directory the in_html_file is from.\n\n",
  194.  "-v             : operate in verbose mode.\n\n",
  195.  "-noclean       : do not remove the old fullbook.htm{l} file and it's\n",
  196.  "                 associated TOC data files. They are removed by default.\n\n",
  197.  "in_html_file   : REQUIRED. Name of the fullbook.htm{l} file to \n",
  198.  "                 process. Also denotes location of the TOC data\n",
  199.  "                 files. Files created will be written to the same\n",
  200.  "                 area, unless otherwise designated\n\n";
  201. }
  202.  
  203.  
  204.  
  205. #
  206. # int readToc(string $tocfile)
  207. #
  208. sub readToc {
  209.  
  210.     my($f) = @_; 
  211.  
  212.     my(@c) = ();
  213.     my($i,$j) = 1;
  214.     my($curr_fname,$s) = '';
  215.     
  216.     open(TOCF, $f) || return 0;
  217.     while(<TOCF>) {
  218.  
  219.           chop;
  220.           $_ =~ s/^\s//g;
  221.           $_ =~ s/\s$//g;
  222.           @c = split('\|', $_);
  223.           $c[1] =~ s/^[\ \s]+//;
  224.           $c[1] =~ s/[\ \s]+$//;
  225.  
  226.           # this is the title line
  227.           #
  228.           if( $c[0] eq '0' && $_ttl eq '' ) {
  229.                 $_ttl = $c[1]; 
  230.                 next;
  231.           }
  232.  
  233.           # break at these levels; configure filename to use
  234.           #
  235.           if( ($c[0] + 0) <= $_lvl_id ) {
  236.  
  237.               # store only those that we are breaking on
  238.               #
  239.               push(@_break_elements, $c[2]);
  240.               push(@_break_titles,   $c[1]);
  241.  
  242.               $curr_fname = sprintf($_fname_spec, $i++); 
  243.           }
  244.  
  245.           # use as quick lookup to determine what each id is mapped to
  246.           #
  247.           $s = $curr_fname . ($c[2] =~ /^#/ ? '' : '#') . $c[2];
  248.           $_element_map{"$c[2]"} = $s;
  249.  
  250.  
  251.           # back of the book index
  252.           #
  253.           if( $c[1] =~ /^Index$/i ) {
  254.               $_indxExist = $curr_fname;
  255.           }
  256.     }
  257.  
  258.     close(TOCF);
  259.     return 1;
  260. }
  261.  
  262.  
  263.  
  264. #
  265. # int createContentFiles(string $mainHtml)
  266. #
  267. sub createContentFiles {
  268.  
  269.     my($f) = @_;
  270.  
  271.     my($id, $curr_fname, $s, $tmp) = '';
  272.     my($i, $j) = 0;
  273.     my($curr_ptr) = -1;
  274.  
  275.     open(FP, $f) || return 0;
  276.     while(<FP>) {
  277.  
  278.           if( $_ =~ /^<\!\-\-\ SGIEND\:/ ) {
  279.  
  280.               if( $curr_fname ne '' ) {
  281.                     $_curr_title = $_break_titles[$curr_ptr];
  282.                     $s = &std_hdrftr($curr_ptr, $STD_FTR);
  283.                     print FP_DOC $s;
  284.                     close(FP_DOC);
  285.               }
  286.  
  287.               last;
  288.           }
  289.  
  290.           # found a marker, start of a section
  291.           #
  292.           if( $_ =~ /^<\!\-\-\ SGITOC\:/ &&  $_ =~ /\|(SGI_[\d]+)/ ) {
  293.  
  294.               $id = $1;
  295.  
  296.               # see if we need to break and create a new file
  297.               #
  298.               for( $i=0, $j=-1; $i < (@_break_elements + 0); $i++ ) {
  299.  
  300.                    if($_break_elements[$i] eq $id
  301.                       ||
  302.                       $_break_elements[$i] eq "#$id" ) {
  303.                         $j = $i;
  304.                         $i = (@_break_elements + 0);
  305.                    }
  306.               }
  307.  
  308.               if( $j != -1 ) {
  309.  
  310.                   if( $curr_fname ne '' ) {
  311.                         $_curr_title = $_break_titles[$curr_ptr];
  312.                         $s = &std_hdrftr($curr_ptr, $STD_FTR);
  313.                         print FP_DOC $s;
  314.                         close(FP_DOC);
  315.                   }
  316.  
  317.                   $curr_ptr   = $j;
  318.                   $curr_fname = $_output_dir . '/' . 
  319.                                 $_element_map{"$_break_elements[$j]"};
  320.                   $curr_fname =~ s/\#\w+$//;
  321.  
  322.                   if( $_bVerbose == 1 ) {
  323.                         print "splithtml: creating $curr_fname\n\n";
  324.                   }
  325.  
  326.                   open(FP_DOC, "> $curr_fname") || return 0; 
  327.                   $_curr_title = $_break_titles[$curr_ptr];
  328.                   $s = &std_hdrftr($curr_ptr, $STD_HDR);
  329.                   print FP_DOC $s;
  330.               }
  331.           }
  332.  
  333.           # adjust all element references
  334.           #
  335.           if( $curr_fname ne '' ) {
  336.  
  337.                 foreach $s (keys %_element_map) {
  338.                    $tmp = $_element_map{$s};
  339.                    $_ =~ s/\"$s\"/\"$tmp\"/g; 
  340.                 }
  341.                 print FP_DOC $_;
  342.           }
  343.     }
  344.     close(FP);
  345.  
  346.     return 1;
  347. }
  348.  
  349.  
  350.  
  351. #
  352. # int createTocFiles()
  353. #
  354. sub createTocFiles {
  355.  
  356.     my(@c) = ('default', 'figures', 'tables', 'examples');
  357.     my(@tocf,@copy_tocf) = ();
  358.     my($i,$j) = 1;
  359.     my($str,$tmp,$pat,$f,$s,$exp_s,$exp_f,$tt) = '';
  360.  
  361.     foreach (@c) {
  362.        $s = $_input_dir . '/' . $_ . '.dat';
  363.        if( -r $s ) {
  364.            push(@tocf, $_);
  365.            push(@copy_tocf, $_);
  366.        }
  367.     }
  368.  
  369.     foreach $tt (@tocf) {
  370.  
  371.        $s = "$_input_dir/$tt" . '.dat';
  372.        open(TOC_IN_F, $s) || return 0;
  373.  
  374.        $f = "$_output_dir/" . ($tt eq "default" ? 'index' : $tt) . '.html';
  375.        open(TOC_OUT_F, "> $f") || return 0;
  376.        if( $_bVerbose == 1 ) {
  377.            print "splithtml: creating $f\n\n";
  378.        }
  379.  
  380.        if( $tt eq "default" ) {
  381.  
  382.            # setup an expanded toc file
  383.            #
  384.            $exp_f = "$_output_dir/toc_full.html";
  385.            open(TOC_EXP_OUT_F, "> $exp_f") || return 0;
  386.        }
  387.  
  388.        $_curr_title = $TOC_TITLES{$tt};
  389.        $s = &std_hdrftr(1, $STD_TOC_HDR);
  390.  
  391.        if( $_bHelpExist == 1 ) {
  392.            $tmp = "<A HREF=\"help.html\">" . $TOC_TITLES{'help'} . "</A>"; 
  393.        } else {
  394.            $tmp = " ";
  395.        }
  396.        $s =~ s/%%LHELP/$tmp/g;
  397.  
  398.        if( $_indxExist ne '' ) {
  399.            $tmp = "<A HREF=\"$_indxExist\">" . $TOC_TITLES{'indx'} . "</A>"; 
  400.        } else {
  401.            $tmp = " ";
  402.        }
  403.        $s =~ s/%%LINDX/$tmp/g;
  404.  
  405.        foreach $f (@copy_tocf) {
  406.           if( $f ne $tt ) {
  407.               $tmp = "<A HREF=\"" . ($f eq "default" ? 'index' : $f) .
  408.                      ".html\">" . $TOC_TITLES{$f} . "</A>"; 
  409.           } else {
  410.               $tmp = $TOC_TITLES{$f};
  411.           }
  412.           $pat = '%%L' . uc($f);
  413.           $s =~ s/$pat/$tmp/g;
  414.        }
  415.  
  416.        if( $tt eq "default" ) {
  417.  
  418.            $s =~ s/%%LBORDER/1/;
  419.            $exp_s = $s;
  420.  
  421.            $tmp = "<A HREF=\"toc_full.html\">" . $TOC_TITLES{'expand'} ."</A>";
  422.            $s =~ s/%%LEXPAND/$tmp/;
  423.            $tmp = $TOC_TITLES{'collapse'};
  424.            $s =~ s/%%LCOLLAPSE/$tmp/;
  425.  
  426.            $tmp = "<A HREF=\"index.html\">" . $TOC_TITLES{'collapse'} ."</A>";
  427.            $exp_s =~ s/%%LCOLLAPSE/$tmp/;
  428.            $tmp = $TOC_TITLES{'expand'};
  429.            $exp_s =~ s/%%LEXPAND/$tmp/;
  430.  
  431.            $exp_s =~ s/%%L\w+//g;
  432.            print TOC_EXP_OUT_F $exp_s;
  433.  
  434.        } else {
  435.            $s =~ s/%%LBORDER/0/;
  436.        }
  437.  
  438.        $s =~ s/%%L\w+//g;
  439.        print TOC_OUT_F $s;
  440.  
  441.        while(<TOC_IN_F>) {
  442.  
  443.              chop;
  444.              @c = split('\|', $_);
  445.  
  446.              if( $c[0] eq '0' ) {
  447.                    next;
  448.              }
  449.  
  450.              # see if file actually exists, if not, do not print this item
  451.              #
  452.              $str = $_element_map{$c[2]};
  453.              if( $str eq '' ) {
  454.                    next;
  455.              }
  456.              
  457.              $str =~ s/\#[\w]+$//;
  458.              $str = "$_output_dir/" . $str;
  459.  
  460.              if( -r $str ) {
  461.                   if( $tt eq "default" && ($c[0] + 0) == 1 ) {
  462.                       print TOC_OUT_F     "\n<BR>\n";
  463.                       print TOC_EXP_OUT_F "\n<BR>\n";
  464.                   }
  465.  
  466.                   if( $tt eq "default" ) {
  467.  
  468.                       if( ($c[0] + 0) < $DEF_EXP_LEVEL ) {
  469.                           print TOC_OUT_F 
  470.                                   ('    ' x ($c[0] + 0)),
  471.                                   "<A HREF=\"", $_element_map{$c[2]}, "\">",
  472.                                   $c[1], "</A><BR>\n";
  473.                       }
  474.                       print TOC_EXP_OUT_F
  475.                                   ('    ' x ($c[0] + 0)),
  476.                                   "<A HREF=\"", $_element_map{$c[2]}, "\">",
  477.                                   $c[1], "</A><BR>\n";
  478.  
  479.                   } else {
  480.                     print TOC_OUT_F ('    ' x ($c[0] + 0)),
  481.                                     "<A HREF=\"", $_element_map{$c[2]}, "\">",
  482.                                     $c[1], "</A><BR>\n";
  483.                   }
  484.              }
  485.        }
  486.        close(TOC_IN_F);
  487.  
  488.        $s = &std_hdrftr(1, $STD_TOC_FTR);
  489.        print TOC_OUT_F $s;
  490.        close(TOC_OUT_F);
  491.  
  492.        if( $tt eq "default" ) {
  493.            print TOC_EXP_OUT_F $s;
  494.            close(TOC_EXP_OUT_F);
  495.        }
  496.     }
  497.  
  498.     return 1;
  499. }
  500.  
  501.  
  502.  
  503. #
  504. # pick up any helpid's and create a simple structure
  505. #
  506. sub createHelpTopics {
  507.  
  508.    my($tmp,$s) = '';
  509.    $tmp = $_fname_spec;
  510.    $tmp =~ s/\%[\d]*d/\*/g;
  511.  
  512.    $s = "grep 'SGI_HELPID:' $_output_dir/$tmp";
  513.    open(HELP_IN,  "$s |") || return;
  514.  
  515.    $s = "$_output_dir/help.html";
  516.    open(HELP_OUT, "> $s") || return;
  517.  
  518.    if( $_bVerbose == 1 ) {
  519.        print "splithtml: creating help topics file ($s)\n\n";
  520.    }
  521.  
  522.    $s = $STD_HDR;
  523.    $s =~ s/%%BK_TITLE/$_ttl/g;
  524.    $s =~ s/%%CURR_TITLE/$TOC_TITLES{'help'}/g;
  525.    $s =~ s/%%PREV_URL/ /g;
  526.    $s =~ s/%%NEXT_URL/ /g;
  527.    $s .= "\n\n<UL>\n";
  528.    print HELP_OUT $s;
  529.  
  530.    my($fname, $id, $title, $ht_set) = '';
  531.  
  532.    while(<HELP_IN>) {
  533.          chop;
  534.          ($fname, $id, $title) = '';
  535.  
  536.          if( $_ =~ /\/([\w\.]+):/ ) {
  537.              $fname = $1;
  538.          }
  539.          if( $_ =~ /\ NAME=\"([\ \!#-~]+)\"/ ) {
  540.              $id = $1;
  541.          }
  542.          if( $_ =~ /\/A>([\ -~]+)/ ) {
  543.              $title = $1;
  544.              $title =~ s/<[\w]{1,4}>//g;
  545.              $title =~ s/<[\/]{1}[\w]{1,4}>//g;
  546.          }
  547.          
  548.          print HELP_OUT "<LI><A href=\"$fname#$id\">$title</A></LI>\n"; 
  549.  
  550.          $ht_set .= "<!-- " . "$id|$fname#$id|$title" . " -->\n";
  551.          $found_one = 1;
  552.    }
  553.    close(HELP_IN);
  554.  
  555.    $s = "\n</UL>\n\n<!-- START SGI_HELPTOPICS -->\n" . $ht_set .
  556.         "<!-- END SGI_HELPTOPICS -->\n\n";
  557.    $s .= $STD_FTR;
  558.    $s =~ s/%%BK_TITLE/$_ttl/g;
  559.    $s =~ s/%%CURR_TITLE/$TOC_TITLES{'help'}/g;
  560.    $s =~ s/%%PREV_URL/ /g;
  561.    $s =~ s/%%NEXT_URL/ /g;
  562.    print HELP_OUT $s;
  563.  
  564.    close(HELP_OUT);
  565.  
  566.    # no help topics found
  567.    #
  568.    if( $ht_set eq '' ) {
  569.        unlink("$_output_dir/help.html");
  570.    } else {
  571.        $_bHelpExist = 1;
  572.    }
  573. }
  574.  
  575.  
  576.  
  577. #
  578. # header and footer methods
  579. #
  580. sub std_hdrftr {
  581.  
  582.     my($s, $in_buf)  = @_;
  583.     my($id) = ($s + 0);
  584.     my($str, $tmp,$buf) = '';
  585.  
  586.     $buf = $in_buf;
  587.     $buf =~ s/%%BK_TITLE/$_ttl/g;
  588.     $buf =~ s/%%CURR_TITLE/$_curr_title/g;
  589.     
  590.     if($id == 0) {
  591.        $tmp = ' '; 
  592.     } else {
  593.        $str = $_element_map{"$_break_elements[($id - 1)]"};
  594.        $str =~ s/\#[\w]+$//;
  595.        $tmp = "<A HREF=\"$str\">" . $_break_titles[($id - 1)] . "</A>";
  596.     }
  597.     $buf =~ s/%%PREV_URL/$tmp/g;
  598.  
  599.     if($id >= ((@_break_elements + 0) - 1)) {
  600.        $tmp = ' ';
  601.     } else {
  602.        $str = $_element_map{"$_break_elements[($id + 1)]"};
  603.        $str =~ s/\#[\w]+$//;
  604.        $tmp = "<A HREF=\"$str\">" . $_break_titles[($id + 1)] . "</A>";
  605.     }
  606.     $buf =~ s/%%NEXT_URL/$tmp/g;
  607.  
  608.     return "$buf";
  609. }
  610.  
  611.